// Copyright 2011 Google Inc. All Rights Reserved.

/**
 * @fileoverview This is the root module for displaying the details of a result.
 *
 * @author gavaletz@google.com (Eric Gavaletz)
 */


goog.provide('resultdetail');


goog.require('datastore');
goog.require('feather');
goog.require('goog.dom');
goog.require('netscore');
goog.require('page');
goog.require('results');
goog.require('track');


/**
 * Indicates that the visualization libraries have been loaded and that their
 * dependencies can start.
 *
 * @type {boolean}
 */
resultdetail.visReady = false;


/**
 * Indicates that the data has been loaded.
 *
 * @type {boolean}
 */
resultdetail.dataReady = false;


/**
 * Indicates that the results have already been started.
 *
 * Prevents the results from being displayed twice.
 *
 * @type {boolean}
 */
resultdetail.resultsDone = false;


/**
 * The result data.
 *
 * @type {Object.<string, Object>}
 */
resultdetail.data;


/**
 * Sets the result data.  Usually just used from the server.
 *
 * @param {Object.<string, Object>} o the data to be stored.
 */
resultdetail.setData = function(o) {
  resultdetail.data = o;
  datastore.json = o['Client'];
  resultdetail.server = o['Server'];
  resultdetail.dataReady = true;
  if (resultdetail.visReady && !resultdetail.resultsDone) {
    resultdetail.displayResults();
  }
};
//used by the closure compiler to eport this function call
goog.exportProperty(window, 'resultdetailSetData', resultdetail.setData);


/**
 * http://code.google.com/apis/loader/
 *
 * This is some tricky stuff and the google.load is not well documented and does
 * some strange things if you are not careful.  Always list the callback
 * function in optional parameters.  See this message for an explination:
 *
 * http://groups.google.com/group/google-maps-api/msg/936506d83fea4458
 */
resultdetail.loadLibs = function() {
  google.load('visualization', '1',
      {'callback': resultdetail.visualizationLoaded,
        'packages': ['corechart', 'table']});
};


/**
 * The primary entry point for the result details.
 */
resultdetail.start = function() {
  track.trackMe(track.trackingCode);
  resultdetail.loadLibs();
};
//used by the closure compiler to eport this function call
goog.exportProperty(window, 'resultdetailStart', resultdetail.start);


/**
 * Callback function for loading the charts API JavaScript library.
 *
 * Sets the indicator variable letting other functions know that the charts API
 * is loaded and if the test results are complete we can start displaying the
 * results..
 */
resultdetail.visualizationLoaded = function() {
  resultdetail.visReady = true;
  if (resultdetail.dataReady && !resultdetail.resultsDone) {
    resultdetail.displayResults();
  }
};


/**
 * Displays the results from the worklist in netscore.
 */
resultdetail.displayResults = function() {
  resultdetail.resultsDone = true;
  page.resultsSetup();
  if (page.loggedInAdmin) {
    track.results();
  }
  for (i in netscore.workList) {
    netscore.workList[i].results();
  }
};
